/*
#include <math.h>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;

struct ListNode{
	int val;
	ListNode  *next;
	ListNode(int x):val(x),next(NULL){}
};
ListNode * buildListNode(int *num,int n){
	ListNode *head=NULL,*p,*q;
	for(int i=0;i<n;i++){
		q=new ListNode(num[i]);
		if(head){
			p->next=q;
		}else{
			head=q;
		}
		p=q;
	}
	return head;
}
void print(ListNode *p){
	while(p){
		cout<<p->val<<",";
		p=p->next;
	}
	cout<<endl;
}
ListNode* swapPairs(ListNode* head) {
	ListNode *newHead=NULL,*tail=NULL,*p,*q,*w;
	if(head==NULL||head->next==NULL)
		return head;
	newHead=head->next;
	p=head;q=p->next;
	while(p&&q){
		w=q->next;
		p->next=q->next;
		q->next=p;
		if(tail)
			tail->next=q;
		tail=p;
		p=w;
		if(w)
			q=w->next;
	}
	if(p)
		tail->next=p;
	return newHead;
}
ListNode* reverseKGroup(ListNode* head, int k) {
	ListNode *newHead=NULL,*spilt,*begin=NULL,*end=NULL,*tail=NULL,*p,*q,*w,*x;
	if(head==NULL||head->next==NULL||k==1)
		return head;
	begin=head;
	while(begin!=NULL){
		end=begin;
		for(int i=0;i<k-1&&end!=NULL;i++){
				end=end->next;
		}
		if(end==NULL){
			if(tail!=NULL){
					tail->next=begin;
			}
			if(newHead==NULL)
				newHead=head;
			break;
		}
		if(newHead==NULL)
			newHead=end;
		spilt=end->next;
		for(p=begin,q=p->next;p!=end;p=w,q=x){
			w=q;
			x=q->next;
			q->next=p;
		}
		begin->next=NULL;
		if(tail!=NULL){
			tail->next=end;
		}
		tail=begin;
		begin=spilt;
	}
	return newHead;
}
int main(){
	int numbers[]={1,2,6,7,8,0};
	int numbers2[]={4,5,11};
	int numbers3[]={3,8,90};
	vector<ListNode*>lists;
	ListNode *l1,*l2,*l3,*head;
	l1=buildListNode(numbers,sizeof(numbers)/sizeof(int));
	lists.push_back(l1);
	l2=buildListNode(numbers2,sizeof(numbers2)/sizeof(int));
	lists.push_back(l2);
	l3=buildListNode(numbers3,sizeof(numbers3)/sizeof(int));
	lists.push_back(l3);

	head=reverseKGroup(l1,8);
	print(head);

	system("pause");
	return 0;
}
*/